package com.matrix.kernel.util;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 年费相关计算
 *
 * @author wujiantao
 * @date 2017/6/6.
 */
public class AnnualUtils {
    /**
     * 计算年费
     * @param annualYear 年度
     * @param patentType 类型：1、开发；2、实用；3、外观
     * @return
     */
    public static float getAnnualFeeAmount(int annualYear, int patentType) {
        BigDecimal bgAnnualFeeAmount = new BigDecimal(0);
        if (patentType == 1) {
            if (annualYear >= 1 && annualYear <= 3) {
                bgAnnualFeeAmount = new BigDecimal(900);
            }

            if (annualYear >= 4 && annualYear <= 6) {
                bgAnnualFeeAmount = new BigDecimal(1200);
            }

            if (annualYear >= 7 && annualYear <= 9) {
                bgAnnualFeeAmount = new BigDecimal(2000);
            }

            if (annualYear >= 10 && annualYear <= 12) {
                bgAnnualFeeAmount = new BigDecimal(4000);
            }

            if (annualYear >= 13 && annualYear <= 15) {
                bgAnnualFeeAmount = new BigDecimal(6000);
            }

            if (annualYear >= 16 && annualYear <= 20) {
                bgAnnualFeeAmount = new BigDecimal(8000);
            }
        }
        else if (patentType == 2 || patentType == 3) {
            if (annualYear >= 1 && annualYear <= 3) {
                bgAnnualFeeAmount = new BigDecimal(600);
            }

            if (annualYear >= 4 && annualYear <= 5) {
                bgAnnualFeeAmount = new BigDecimal(900);
            }

            if (annualYear >= 6 && annualYear <= 8) {
                bgAnnualFeeAmount = new BigDecimal(1200);
            }

            if (annualYear >= 9 && annualYear <= 10) {
                bgAnnualFeeAmount = new BigDecimal(2000);
            }
        }
        return bgAnnualFeeAmount.floatValue();
    }

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

    /**
     * 计算滞纳金
     *
     * @param payDate 缴费截止日
     * @param patentType 专利类型
     * @param annualYear 年费年度
     * @return
     */
    public static float getOverdueFee(Date payDate, int patentType, int annualYear)
    {
        Calendar calPayTime = Calendar.getInstance();
        Calendar calCurTime = Calendar.getInstance();

        float overdueFee = 0;
        try {
            calCurTime.setTime(sdf.parse(sdf.format(new Date())));
            int year = calCurTime.get(Calendar.YEAR );
            int month = calCurTime.get(Calendar.MONTH )+ 1;
            int day = calCurTime.get(Calendar.DAY_OF_MONTH);

            calPayTime.setTime(sdf.parse(sdf.format(payDate)));
            int realYear = calPayTime.get(Calendar.YEAR);
            int realMonth = calPayTime.get(Calendar.MONTH )+ 1;
            int realDay = calPayTime.get(Calendar.DAY_OF_MONTH);

            // 开始滞纳
            if (calPayTime.compareTo(calCurTime) < 0)
            {
                int monthDiff = month - realMonth + (year - realYear) * 12;
                int dayDiff = (day - realDay) <= 0 ? 0 : 1;
                // 滞纳月数如果超过6个月就按最高的算
                int overdueMonth = (monthDiff + dayDiff) >= 6 ? 6 : (monthDiff + dayDiff);
                if (overdueMonth > 0)
                {
                    switch (patentType)
                    {
                        case 1:
                            if (1 <= annualYear && annualYear <= 3)
                            {
                                overdueFee = (overdueMonth - 1) * 45;
                            }
                            else if (4 <= annualYear &&  annualYear <= 6)
                            {
                                overdueFee = (overdueMonth - 1) * 60;
                            }
                            else if (7 <=  annualYear &&  annualYear <= 9)
                            {
                                overdueFee = (overdueMonth - 1) * 100;
                            }
                            else if (10 <=  annualYear &&  annualYear <= 12)
                            {
                                overdueFee = (overdueMonth - 1) * 200;
                            }
                            else if (13 <=  annualYear &&  annualYear <= 15)
                            {
                                overdueFee = (overdueMonth - 1) * 300;
                            }
                            else if (16 <=  annualYear &&  annualYear <= 20)
                            {
                                overdueFee = (overdueMonth - 1) * 400;
                            }
                            break;
                        case 2:
                        case 3:
                            if (1 <=  annualYear &&  annualYear <= 3)
                            {
                                overdueFee = (overdueMonth - 1) * 30;
                            }
                            else if (4 <=  annualYear &&  annualYear <= 5)
                            {
                                overdueFee = (overdueMonth - 1) * 45;
                            }
                            else if (6 <=  annualYear &&  annualYear <= 8)
                            {
                                overdueFee = (overdueMonth - 1) * 60;
                            }
                            else if (9 <=  annualYear &&  annualYear <= 10)
                            {
                                overdueFee = (overdueMonth - 1) * 100;
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        catch (ParseException e)
        {
            e.printStackTrace();
        }
        return overdueFee;
    }

    /**
     * 根据缴费日期计算当前年费状态
     *
     * @param payDate 缴费截止日
     * @param applicationDate 申请日
     * @param patentType 专利类型
     * @return 1：正常；2：紧急；3：滞纳；4：失效
     */
    public static int getAnnualStatus(Date payDate, Date applicationDate, int patentType)
    {
        Calendar calPayTime = Calendar.getInstance();
        Calendar calCurTime = Calendar.getInstance();
        Calendar calAppTime = Calendar.getInstance();

        // 发明专利20年，其他10年
        int years = patentType == 1 ? 20 : 10;
        int annualStatus = 1;
        try
        {
            calCurTime.setTime(sdf.parse(sdf.format(new Date())));
            int year = calCurTime.get(Calendar.YEAR);
            int month = calCurTime.get(Calendar.MONTH) + 1;
            int day = calCurTime.get(Calendar.DAY_OF_MONTH);

            calPayTime.setTime(sdf.parse(sdf.format(payDate)));
            int realYear = calPayTime.get(Calendar.YEAR);
            int realMonth = calPayTime.get(Calendar.MONTH) + 1;
            int realDay = calPayTime.get(Calendar.DAY_OF_MONTH);

            calAppTime.setTime(sdf.parse(sdf.format(applicationDate)));
            int appYear = calPayTime.get(Calendar.YEAR);
            int appMonth = calPayTime.get(Calendar.MONTH) + 1;
            int appDay = calPayTime.get(Calendar.DAY_OF_MONTH);

            // 根据月日计算是否有一年
            int appYearDiff = month - appMonth;
            if (appYearDiff > 0)
                appYearDiff = 1;
            else if (appYearDiff < 0)
                appYearDiff = -1;
            else if (appYearDiff == 0)
                appYearDiff = (day > appDay) ? 1 : -1;

            // 判断是否失效 超过20年或者大于六个月
            if (((year - appYear) + appYearDiff) > years)
            {
                annualStatus = 4;
                return annualStatus;
            }

            // 开始滞纳
            if (calPayTime.compareTo(calCurTime) < 0)
            {
                // 计算缴费截止日是否差6个月-失效
                int expiresYear = year - realYear < 0 ? 0 : year - realYear;
                int payMonthDiff = expiresYear * 12 + month - realMonth +  (day - realDay > 0 ? 1 : 0);
                if (payMonthDiff > 6)
                {
                    annualStatus = 4;
                    return annualStatus;
                }

                // 滞纳
                annualStatus = 3;
                return annualStatus;
            }

            // 还没有滞纳的话，计算正常还是紧急
            // 时间身后推一个月：如果当前是12月，推到第二年
            if (month == 12)
            {
                year++;
                month = 1;
            }
            else
            {
                month++;
            }

            // 推后的日期年大于缴费年，说明在一个月内紧急
            if (year > realYear)
            {
                annualStatus = 2;
                return annualStatus;
            }
            // 年不变的话，计算月份
            if (year == realYear)
            {
                if (month > realMonth)
                {
                    annualStatus = 2;
                    return annualStatus;
                }
                // 月份一样，计算日
                if (month == realMonth)
                {
                    if (day > realDay)
                    {
                        annualStatus = 2;
                        return annualStatus;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        return annualStatus;
    }
}
